package com.cyjt.operation.ui;

import java.util.ArrayList;
import java.util.List;

import com.cyjt.operation.R;
import com.cyjt.operation.adapter.ViewPagerAdaper;
import com.cyjt.operation.fragment.WorkOrderManagementFinishedFragment;
import com.cyjt.operation.fragment.WorkOrderManagementUnfinishedFragment;
import com.cyjt.operation.view.ActionBarLayout;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

/**
 * 工单管理的Activity
 * 
 * @author LTP
 *
 */
public class WorkOrderManagementActivity extends FragmentActivity {

	private ViewPager viewPager_work_order_management_switch;
	/** ViewPager的Tab标题 */
	private TextView button_work_order_management_finished, button_work_order_management_unfinished;
	// /** 用于ViewPager切换时底部的动画显示线条 */
	// private View view_work_order_management_switch;
	/** ViewPager的Fragment集合 */
	private List<Fragment> fragmentList = new ArrayList<Fragment>();
	/** 已完成工单的Fragment */
	private WorkOrderManagementFinishedFragment mWorkOrderManagementFinishedFragment;
	/** 未完成工单的Fragment */
	private WorkOrderManagementUnfinishedFragment mWorkOrderManagementUnfinishedFragment;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_work_order_management);
		initView();
		initViewPager();
	}

	/**
	 * 控件初始化
	 */
	private void initView() {
		ActionBarLayout.setTitle("工单管理");
		mWorkOrderManagementFinishedFragment = new WorkOrderManagementFinishedFragment();
		mWorkOrderManagementUnfinishedFragment = new WorkOrderManagementUnfinishedFragment();
		fragmentList.add(mWorkOrderManagementFinishedFragment);
		fragmentList.add(mWorkOrderManagementUnfinishedFragment);
		button_work_order_management_finished = (TextView) findViewById(R.id.button_work_order_management_finished);
		button_work_order_management_finished.setOnClickListener(new MyOnClickListener(0));
		button_work_order_management_unfinished = (TextView) findViewById(R.id.button_work_order_management_unfinished);
		button_work_order_management_unfinished.setOnClickListener(new MyOnClickListener(1));
		// view_work_order_management_switch =
		// findViewById(R.id.view_work_order_management_switch);

	}

	/**
	 * ViewPager的初始化
	 */
	private void initViewPager() {
		// TODO Auto-generated method stub
		viewPager_work_order_management_switch = (ViewPager) findViewById(R.id.viewPager_work_order_management_switch);
		ViewPagerAdaper mViewPagerAdaper = new ViewPagerAdaper(getSupportFragmentManager(), fragmentList);
		viewPager_work_order_management_switch.setAdapter(mViewPagerAdaper);
		viewPager_work_order_management_switch.setOnPageChangeListener(new OnPageChangeListener() {

			@Override
			public void onPageSelected(int position) {
				// TODO 页面切换的回调
				// viewMove(position);
				switch (position) {
				case 0:
					button_work_order_management_finished.setBackgroundResource(R.drawable.collect_tab_left_btn_sel);
					button_work_order_management_finished.setTextColor(getResources().getColor(R.color.white));
					button_work_order_management_unfinished.setBackgroundResource(R.drawable.collect_tab_right_btn);
					button_work_order_management_unfinished.setTextColor(getResources().getColor(R.color.black));
					break;
				case 1:
					button_work_order_management_finished.setBackgroundResource(R.drawable.collect_tab_left_btn);
					button_work_order_management_finished.setTextColor(getResources().getColor(R.color.black));
					button_work_order_management_unfinished.setBackgroundResource(R.drawable.collect_tab_right_btn_sel);
					button_work_order_management_unfinished.setTextColor(getResources().getColor(R.color.white));
					break;
				default:
					break;
				}
			}

			@Override
			public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onPageScrollStateChanged(int state) {
				// TODO Auto-generated method stub

			}
		});
	}

	/**
	 * 设置ViewPager自定义标题文本的点击事件
	 * 
	 * @author LTP
	 */
	private class MyOnClickListener implements OnClickListener {
		private int index = 0;

		public MyOnClickListener(int i) {
			index = i;
		}

		@Override
		public void onClick(View v) {
			// 设置点击后跳转的选项卡
			viewPager_work_order_management_switch.setCurrentItem(index);
		}
	}

	/**
	 * // * 移动ViewPager的Tab下的指示线条 // * // * @param moveToTab // *
	 * 目标Tab，也就是要移动到的导航选项按钮的位置 第一个导航按钮对应0，第二个对应1，以此类推 //
	 */
	// private void viewMove(int moveToTab) {
	// int startPosition = 0;
	// int movetoPosition = 0;
	// int screenWidth = getResources().getDisplayMetrics().widthPixels;
	//
	// startPosition = viewPager_work_order_management_switch.getCurrentItem()
	// * (screenWidth / 3);
	// movetoPosition = moveToTab * (screenWidth / 3);
	// // 平移动画
	// TranslateAnimation translateAnimation = new TranslateAnimation(
	// startPosition, movetoPosition, 0, 0);
	// translateAnimation.setFillAfter(true);
	// translateAnimation.setDuration(200);
	// view_work_order_management_switch.startAnimation(translateAnimation);
	// }
}
